<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  <link rel="stylesheet" title="Default" href="http://acm.math.spbu.ru/~sk1/colorer/my.css">

  <script src="http://acm.math.spbu.ru/~sk1/colorer/highlight.js"></script>
  <script src="http://acm.math.spbu.ru/~sk1/colorer/cpp.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>

<body>

<pre><code>
/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com5
 * Date: 2013.10.18
 */

#include &lt;cstdio&gt;
#include &lt;cassert&gt;
#include &lt;algorithm&gt;
#include &lt;set&gt;

using namespace std;

#define forn(i, n) for (int i = 0; i &lt; (int)(n); i++)
#define mp make_pair

typedef pair &lt;int, int&gt; pii;

typedef long long ll;

const int N = (int)1e5;

int pn, n, m, x[N], y[N], z[N], p[N];
pii pa[N];

ll sum;
set &lt;pii&gt; s;

inline bool xless( int i, int j )
{
  return x[i] &gt; x[j];
}

void add( int y, int z )
{
  set &lt;pii&gt;::iterator l, r, it = s.lower_bound(mp(y, z));
  if (it-&gt;second &gt;= z)
    return;
  pn = 0;
  for (it--; it-&gt;second &lt;= z; it--)
    pa[pn++] = *it;
  while (pn)
  {
    l = r = it = s.find(pa[--pn]);
    l--, r++;
    sum -= (it-&gt;first - l-&gt;first) * (it-&gt;second - r-&gt;second);
    s.erase(it);
  }
  s.insert(mp(y, z));

  l = r = it = s.find(mp(y, z));
  l--, r++;
  sum += (it-&gt;first - l-&gt;first) * (it-&gt;second - r-&gt;second);
}

int main()
{
  while (scanf("%d%d", &n, &m) == 2 && (n || m))
  {
    forn(i, n)
      scanf("%d%d%d", &x[i], &y[i], &z[i]), p[i] = i;
    sort(p, p + n, xless);

    ll ans = (ll)m * m * m;
    int cx = -1;
    sum = 0;
    s.clear();
    s.insert(mp(0, m + 1));
    s.insert(mp(m + 1, 0));
    for (int i = 0, j; i &lt; n; i = j)
    {
      for (j = i; j &lt; n && x[p[i]] == x[p[j]]; j++)
        ;
      if (cx != -1)
        ans -= sum * abs(x[p[i]] - cx);
      for (int k = i; k &lt; j; k++)
        add(y[p[k]], z[p[k]]);
      cx = x[p[i]];
    }
    if (cx != -1)
      ans -= sum * cx;
    printf("%I64d\n", ans);
  }
  return 0;
}

</code></pre>

</body>
</html>

<font style="visibility:hidden">
